package cn.shujuhai.common.mapper;

import cn.shujuhai.common.domain.SysRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author NiuYahu
 * @description 针对表【sys_role(角色表)】的数据库操作Mapper
 * @createDate 2024-10-17 14:37:55
 * @Entity cn.shujuhai.domain.SysRole
 */
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRole> {
    @Select("SELECT role_name AS roleName, role_code AS roleCode FROM sys_role")
    List<Map<String, Object>> selectAllRoleNamesAndCodes();

    /**
     * 根据角色ID查询用户名
     *
     * @param roleId 角色ID
     * @return 用户名列表
     */
    @Select("SELECT DISTINCT u.username " +
            "FROM sys_user u " +
            "INNER JOIN sys_user_role ur ON u.user_id = ur.user_id " +
            "WHERE ur.role_id = #{roleId} " +
            "AND ur.deleted <> 1 " +
            "AND u.deleted <> 1 " +
            "UNION " +
            "SELECT DISTINCT u.username " +
            "FROM sys_user u " +
            "INNER JOIN role_department rd ON u.user_id = rd.user_id " +
            "WHERE rd.role_id = #{roleId} " +
            "AND rd.deleted <> 1 " +
            "AND u.deleted <> 1")
    List<String> findUsernamesByRoleId(@Param("roleId") Long roleId);
}






